﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using You2V.Data.Shared;
using You2V.LocalDBLayer;

namespace You2VTool.Controls
{
    /// <summary>
    /// Interaction logic for StudentMain.xaml
    /// </summary>
    public partial class StudentMain : UserControl
    {
        public StudentMain()
        {
            InitializeComponent();
            cb_StudentFilter.ItemsSource = FilterList;
            if (FilterList.Count > 0)
                cb_StudentFilter.SelectedIndex = 0;

            LoadStudentList();

        }

        private List<Student> _studentList;

        private string SQLWhere_FromFilter
        {
            get
            {
                if (cb_StudentFilter.SelectedItem != null)
                {
                    StudentFilter StudentFilter = (StudentFilter)cb_StudentFilter.SelectedItem;
                    return StudentFilter.SQLWhere;
                }
                else
                {
                    return "1=1";
                }
            }
        }

        private void LoadStudentList()
        {
            StudentDBL StudentDBL = new StudentDBL();
            _studentList = StudentDBL.GetStudentListGiventWhereCondition(SQLWhere_FromFilter);

            dg_Student.ItemsSource = _studentList;
        }


        private void dg_Student_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Student SelectedStudent = (Student)this.dg_Student.SelectedItem;

            if (SelectedStudent != null)
            {
                //TODO, 保险起见重新读入一次学生信息，因为之前操作过的部分可能在该类中cache住了没有更新
                //有待和孔同学讨论

                p_studentAction.ReLocateStudent(SelectedStudent);

                p_studentInfo2012.LoadStudent(SelectedStudent.tc_info, false);
            }
        }

        private void TriggerSearch()
        {
            string keyWord = this.tb_Search.Text;

            if (string.IsNullOrWhiteSpace(keyWord))
            {
                LoadStudentList();
                return;
            }

            string strWhere = " 1=1 ";
            if (!string.IsNullOrWhiteSpace(keyWord))
            {
                strWhere = " StudentNumber like '%" + keyWord + "%' OR FullName like '%" + keyWord + "%' OR NickName like '%" + keyWord + "%' ";
            }
            strWhere = " ( " + strWhere + " ) AND " + SQLWhere_FromFilter;


            StudentDBL StudentDBL = new StudentDBL();
            _studentList = StudentDBL.GetStudentListGiventWhereCondition(strWhere);
            dg_Student.ItemsSource = _studentList;
        }

        private void tb_Search_KeyDown(object sender, KeyEventArgs e)
        {
            // 因为性能差，敲回车才执行
            if (e.Key == Key.Enter)
            {
                TriggerSearch();
            }
        }

        private void bt_Search_Click(object sender, RoutedEventArgs e)
        {
        }

        private void bt_CancelSearch_Click(object sender, RoutedEventArgs e)
        {
            this.tb_Search.Text = string.Empty;
            LoadStudentList();
        }

        #region Filter

        public List<StudentFilter> FilterList
        {
            get
            {
                if (m_FilterList == null)
                    m_FilterList = new List<StudentFilter>();

                if (m_FilterList.Count == 0)
                {
                    m_FilterList.Add(new StudentFilter("全部", "1=1"));
                    m_FilterList.Add(new StudentFilter("蒙山学生", "tc_area='6'"));
                    m_FilterList.Add(new StudentFilter("武宣学生", "tc_area='5'"));
                    m_FilterList.Add(new StudentFilter("东兰学生", "tc_area='11'"));
                }

                return m_FilterList;
            }
        }

        private List<StudentFilter> m_FilterList;
        #endregion

        private void cb_StudentFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LoadStudentList();
        }
    }

    public class StudentFilter
    {
        public string FilterName { get; set; }
        public string SQLWhere { get; set; }

        public StudentFilter()
        {
            FilterName = string.Empty;
            SQLWhere = string.Empty;
        }

        public StudentFilter(string Name, string Where)
        {
            this.FilterName = Name;
            SQLWhere = Where;
        }
    }
}
